
from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil
import types 


class LibeventConan(ConanFile):
    name = "ffmpeg"
    version = "4.2.1"
    url = "https://gitee.com/mirrors/ffmpeg.git"
    description = "ffmpeg"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"
    
    def source(self):
        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            tools.rmdir("./pkgsrc")
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url,branch="release/4.2")

    def build(self):
        incflag = ""
        libflag = ""
        cmake = CMake(self)
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))
        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")

        print(self.deps_cpp_info["fdk-aac"].include_paths)
        print(self.deps_cpp_info["fdk-aac"].lib_paths)

        print(self.deps_cpp_info["libx264"].include_paths)
        print(self.deps_cpp_info["libx264"].lib_paths)

        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                print(type((self.deps_cpp_info["libx264"].include_paths)))
                for i in self.deps_cpp_info["libx264"].include_paths:
                    incflag = incflag + " --extra-cflags=-I" + tools.unix_path(i)
                for i in self.deps_cpp_info["fdk-aac"].include_paths:
                    incflag = incflag + " --extra-cflags=-I" + tools.unix_path(i)
                for i in self.deps_cpp_info["fdk-aac"].lib_paths:
                    libflag = libflag + " --extra-ldflags=-L" +  tools.unix_path(i)
                for i in self.deps_cpp_info["libx264"].lib_paths:
                    libflag = libflag + " --extra-ldflags=-L" + tools.unix_path(i)

                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True,
                                            bashcmd= "./configure  --arch=x86 --target-os=mingw32  --enable-gpl --enable-cross-compile --enable-static" +
                                            " --enable-debug " +
                                            " --disable-encoders --disable-decoders --disable-decoder=vp9 " +
                                            " --disable-decoder=hevc --enable-libx264 " +
                                            " --enable-encoder=libx264 --enable-nonfree --enable-libfdk-aac --enable-decoder=aac " +
                                            " --enable-encoder=libfdk_aac --disable-yasm --disable-asm " +
                                             incflag  +  
                                             libflag + 
                                            " --prefix=" + tools.unix_path(self.package_folder))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="make -j 8")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="make install")
                try:
                    os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
                except BaseException:
                    print("BaseException")                 
            if tools.get_env("BUILD_ENV") == "msvc32":
                print("msvc32 build")
                env_vars = tools.vcvars_dict(self)
                for i in self.deps_cpp_info["libx264"].include_paths:
                    incflag = incflag + " --extra-cflags=-I" + i.replace("\\",'/')
                for i in self.deps_cpp_info["fdk-aac"].include_paths:
                    incflag = incflag + " --extra-cflags=-I" + i.replace("\\",'/')
                for i in self.deps_cpp_info["fdk-aac"].lib_paths:
                    libflag = libflag + " --extra-ldflags=-LIBPATH:" + i.replace("\\",'/')
                for i in self.deps_cpp_info["libx264"].lib_paths:
                    libflag = libflag + " --extra-ldflags=-LIBPATH:" + i.replace("\\",'/')

                with tools.environment_append(env_vars):
                    self.run( "./pkgsrc/configure  --arch=x86 --target-os=mingw32  --enable-gpl --enable-cross-compile --toolchain=msvc --enable-static" +
                                                " --enable-debug  --disable-programs " +
                                                " --disable-encoders --disable-decoders --disable-decoder=vp9 " +
                                                " --disable-decoder=hevc --enable-libx264 " +
                                                " --enable-encoder=libx264 --enable-nonfree --enable-libfdk-aac --enable-decoder=aac " +
                                                " --enable-encoder=libfdk_aac --disable-yasm --disable-asm  --extra-ldflags=libx264.lib " 
                                                +   "--extra-ldflags=fdk-aac.lib " +  
                                                incflag  +  
                                                libflag + " --prefix=" + tools.unix_path(self.package_folder),win_bash=True)

                    self.run("make -j 12",win_bash=True)
                    self.run("make install",win_bash=True)

      

                    
                       
    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)
    def requirements(self):
        self.requires("fdk-aac/1.1.1")
        self.requires("libx264/1.1.1")

    def package_info(self):
        self.cpp_info.name = 'ffmpeg'

        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "ffmpeg"
                self.cpp_info.names["cmake_find_package_multi"] = "ffmpeg"
                self.cpp_info.libdirs = ['pkgsrc']  # Directories where libraries can be found
                self.cpp_info.libs = ['libavcodec.a','libavdevice.a',"libavfilter.a","libavformat.a","libavutil.a","libpostproc.a","libswresample.a","libswscale.a"]  # The libs to link against
                self.cpp_info.buildirs = 'pkgsrc'
            if (tools.get_env("BUILD_ENV") == "msvc32" ) or (tools.get_env("BUILD_ENV") == "msvc64" ):
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "ffmpeg"
                self.cpp_info.names["cmake_find_package_multi"] = "ffmpeg"
                self.cpp_info.libs = ['avcodec.lib','avdevice.lib',
                "avfilter.lib","avformat.lib","avutil.lib","postproc.lib","swresample.lib","swscale.lib"]  # The libs to link against
